// this script manages a xul tree
// it supports creation (tree/row/column), modification(row/column)
// removal(row/column)

function TreeManagerClass() {
	var atree;
	var atreecols;
	var acolumn;
	var asplitter;
	var atreechild;
	var arowitem;
	var arowset;
	var acell;
	this.CreateTree = function(element, treeid, flex, click, select) {
		atree = document.createElement("tree");
		atreecols = document.createElement("treecols");
		atreechild = document.createElement("treechildren");
		atreechild.setAttribute("id", treeid + "-treechildren");
		atreecols.setAttribute("id", treeid + "-treecols");
		atree.setAttribute("id", treeid);
		atree.setAttribute("ondblclick", click);
		atree.setAttribute("onselect", select);
		if (flex > 0) {
			atree.setAttribute("flex", flex);
		} else {
			atree.setAttribute("flex", 1);
		}
		atree.appendChild(atreecols);
		atree.appendChild(atreechild);
		element.appendChild(atree);
	}
	this.AddColumn = function(treeid, id, label, flex, splitter, type) {
		atreecols = $(treeid + "-treecols");
		acolumn = document.createElement("treecol");
		acolumn.setAttribute("id", id);
		acolumn.setAttribute("label", label);
		if (flex > 0) {
			acolumn.setAttribute("flex", flex);
		} else {
			acolumn.setAttribute("flex", 1);
		}
		if (type == "progressmeter") {
			acolumn.setAttribute("type", type);
		}
		atreecols.appendChild(acolumn);
		if (splitter = true) {
			asplitter = document.createElement("splitter");
			asplitter.setAttribute("class", "tree-splitter");
			atreecols.appendChild(asplitter);
		}
	}
	this.RemoveColumn = function(treeid, id) {
		$(treeid).removeChild(document.getElementById(id));
	}
	// dataarray access looks like
	/*
	 * key is one of id, mode, label i is an int dataarray[i][key]
	 */
	this.AddRow = function(treeid, id, dataarray) {
		arowset = document.createElement("treerow");
		arowitem = document.createElement("treeitem");
		arowitem.setAttribute("id", id);
		for (i = 0; i < dataarray.length; i++) {
			acell = document.createElement("treecell");
			acell.setAttribute("label", dataarray[i].label);
			if (dataarray[i].mode == "normal") {
				acell.setAttribute("mode", "normal");
				acell.setAttribute("value", dataarray[i].label);
			}
			if (dataarray[i]['id'] != null) {
				acell.setAttribute("id", dataarray[i].id);
			}
			arowset.appendChild(acell);
		}
		arowitem.appendChild(arowset);
		$(treeid + "-treechildren").appendChild(arowitem);
	}
	this.RemoveRow = function(treeid, id) {
		$(treeid).removeElement(id);
	}
	this.SetRow = function(treeid, row, dataarray) {
	}
	this.GetRow = function(treeid, row, col) {
	}
	this.SetAttribute = function(elementid, attr, val) {
		$(elementid)[attr] = val;
	}
}